var app = getApp();
Page({
  showAllDesc: false,
  data: {
    avatar: "",
    remind: "加载中"
  },
  onHide: function () {
    this.setData({ remind: '' });
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    let that = this;
    that.setData({ remind: '加载中' });
    var id = options.id;
    var avatar = options.avatar;
    var url = app.globalData.doubanBase + app.globalData.celebrity + id;
    that.setData({ "avatar": avatar });
    that.getCelebrityData(url);
  },
  /** 展开简介   */
  handleExtensiontap: function (event) {
    var readyData = {
      "showAllDesc": true
    };
    this.setData(readyData);
  },
  /** 获取影人信息 */
  getCelebrityData: function (url) {
    var that = this;
    wx.request({
      url: url,
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: { 'content-type': 'json' }, // 设置请求的 header
      success: function (res) {
        var data = res.data;
        that.processCelebrityData(data);
        that.setData({ remind: '' });
      },
      fail: function () {
        that.setData({ remind: '404' });
      },
      complete: function () {
      }
    });
  },
  /** 组装影人数据 */
  processCelebrityData: function (data) {
    var movies = [];
    for (let idx in data.works) {
      var subject = data.works[idx].subject;
      movies.push(subject);
    }
    var photos = {};
    data.photos.forEach(function (p, index) {
      photos[index] = p['image'];
    });
    var temp = {
      id: data.id,
      avatars: data.avatars,
      bornPlace: data.born_place,
      gender: data.gender,
      name: data.name,
      name_en: data.name_en,
      movie: movies,
      summary: data.summary || "暂无数据",
      photos: photos
    };
    var readyData = {};
    readyData["celebrity"] = temp;
    this.setData(readyData);
  },
  /** 跳转电影详情页 */
  bindMovieDetail: function (event) {
    var id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/my/movie/movie-detail/movie-detail?id=' + id
    });
  },
  /** 查看海报 */
  bindPoster: function (event) {
    // var posterUrl = event.currentTarget.dataset.posterUrl;
    // wx.navigateTo({
    //   url: '/pages/my/movie/movie-detail/movie-poster/movie-poster?posterUrl=' + posterUrl
    // });
    var photos = this.data.celebrity.photos;
    if (photos) {
      var tmp = [];
      for (var p in photos) {
        tmp.push(photos[p]);
      }
      wx.previewImage({
        current: tmp[event.target.dataset.id],
        urls: tmp
      })
    }
  }
})